<template>
  <div>
    <button @click="startWorker">开始计算</button>
    <div>计算结果：{{ result }}</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      result: null,
      worker: null,
    };
  },
  mounted() {
    const actions = [
      {
        message: 'func1',
        func: (arg) => {
          console.log(arg);
          function add(a, b) {
            return a + b;
          }
          const res = add(arg.a, arg.b);
          console.log(res);
          return res;
        },
      },
    ];
    this.worker = this.$worker.create(actions);
  },
  methods: {
    startWorker() {
      this.worker
        .postMessage('func1', [{ a: 1, b: 2 }])
        .then((res) => (this.result = res))
        .catch(console.error);
    },
  },
};
</script>

